package com.sansecy.mocklocationtest.apklist;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;

import com.sansecy.mocklocationtest.AppInfo;
import com.sansecy.mocklocationtest.R;
import com.sansecy.mocklocationtest.getInstalledApps;
import com.sansecy.mocklocationtest.loadedapk.LaodedApkFragment;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppListActivity extends AppCompatActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, AppListActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_l_ist);
        getSupportFragmentManager().beginTransaction().add(R.id.content, new ApplistFragement()).commit();
    }

    private static final String TAG = "AppListActivity-App";

    /**
     * 通过 Activity 中 Intent.CATEGORY_SAMPLE_CODE 标识，遍历系统中注册该标识的 Activity
     * 根据路径地址，返回封装之后的结果
     *
     * @return
     */
    protected List<Map<String, Object>> getData() {
        List<Map<String, Object>> myData = new ArrayList<>();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

        if (null == list) {
            return myData;
        } else {
            for (ResolveInfo resolveInfo : list) {
                Log.d(TAG, "getData() called with: prefix = [" + resolveInfo.toString() + "]");
            }
        }
//
//        String[] prefixPath;
//        String prefixWithSlash = prefix;
//
//        if (prefix.equals("")) {
//            prefixPath = null;
//        } else {
//            prefixPath = prefix.split("/");
//            prefixWithSlash = prefix + "/";
//        }
//
//        int len = list.size();
//
//        Map<String, Boolean> entries = new HashMap<>();
//
//        for (int i = 0; i < len; i++) {
//            ResolveInfo info = list.get(i);
//            CharSequence labelSeq = info.loadLabel(pm);
//            String label = labelSeq != null
//                    ? labelSeq.toString()
//                    : info.activityInfo.name;
//
//            if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
//
//                String[] labelPath = label.split("/");
//
//                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
//
//                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
//                    addItem(myData, nextLabel, activityIntent(
//                            info.activityInfo.applicationInfo.packageName,
//                            info.activityInfo.name));
//                } else {
//                    if (entries.get(nextLabel) == null) {
//                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
//                        entries.put(nextLabel, true);
//                    }
//                }
//            }
//        }
//
//        Collections.sort(myData, sDisplayNameComparator);

        return myData;
    }

    protected Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        result.setClassName(pkg, componentName);
        return result;
    }

    private final static Comparator<Map<String, Object>> sDisplayNameComparator =
            new Comparator<Map<String, Object>>() {
                private final Collator collator = Collator.getInstance();

                public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                    return collator.compare(map1.get("title"), map2.get("title"));
                }
            };

    //    protected Intent browseIntent(String path) {
//        Intent result = new Intent();
//        result.setClass(this, ApiDemos.class);
//        result.putExtra("com.example.android.apis.Path", path);
//        return result;
//    }
    protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("title", name);
        temp.put("intent", intent);
        data.add(temp);
    }
}